﻿using System;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;
using System.Xml;

namespace DAOFactory
{
	/// <summary>
	/// Description of DBConection.
	/// </summary>
	public class ConnectionManager
	{
		
		private SqlConnection connection;
		private static ConnectionManager connectionManager;
		
		public ConnectionManager()
		{
			this.connection = null;
		}
		
		/*private static void initConnection(){
			
			String cadenaConexion = "";
			
			try{
				FileStream archivo = new FileStream("cadenaConexion.txt", FileMode.Open, FileAccess.Read);
				StreamReader reader = new StreamReader(archivo);
				cadenaConexion = reader.ReadLine();
				reader.Close();
				archivo.Close();
			}
			catch(Exception e){
				MessageBox.Show("Error al leer archivo de cadena de conexión.\n"+e);
				return;
			}
			
			try{
				connectionManager = new ConnectionManager();
				String connStr = cadenaConexion;
				connectionManager.connection = new SqlConnection(connStr);
				connectionManager.connection.Open();	
			}
			catch(Exception e)
			{	
				MessageBox.Show("Error al intentar conectar a la base de datos");
			}
		}*/
		
		public static void closeConnection()
		{
			try
			{
				connectionManager.connection.Close();
			}
			catch
			{	
				//instrucciones dentro del catch
			}	
		}
		
		public static ConnectionManager getInstance(){
			if (connectionManager == null){
				initConnection();
			}
			return connectionManager;
		}
		
		public static SqlConnection getConnection(){
			return getInstance().connection;
		}



        private static void initConnection()
        {
        	string cadenaConexion = "";
        	try{
	            string id = "", password = "", server = "", database = "", connectionTimeOut = "", integratedSecurity = "";
	            string multipleActiveResultSets = "";
	            string packetSize = "", persistSecurityInfo = "", pooling = "", typeSystemVersion = "";
	            XmlDocument archCnx = new XmlDocument();
	            archCnx.Load("cadenaConexion.xml");
	            /*Se carga una lista de nodos de Conexion*/
	            XmlNodeList conexionLST = archCnx.GetElementsByTagName("Conexion");
	            /*Se carga unalista de NodoConexion*/
	            XmlNodeList nodoCnxLST=  ( (XmlElement)conexionLST[0]).GetElementsByTagName("NodoConexion");
	            foreach (XmlElement nodoConexion in nodoCnxLST)
	            {
	                id=nodoConexion.GetAttribute("id");
	                password= nodoConexion.GetAttribute("password");  // nodoConexion.GetAttribute("");
	                server = nodoConexion.GetAttribute("server");
	                database = nodoConexion.GetAttribute("database");
	                connectionTimeOut=  nodoConexion.GetAttribute("connectionTimeout");
//	                integratedSecurity= nodoConexion.GetAttribute("integratedSecurity");
//	                multipleActiveResultSets = nodoConexion.GetAttribute("multipleActiveResultSets");
//	                packetSize= nodoConexion.GetAttribute("packetSize");
//	                persistSecurityInfo = nodoConexion.GetAttribute("persistSecurityInfo");
//	                pooling = nodoConexion.GetAttribute("pooling");
//	                typeSystemVersion= nodoConexion.GetAttribute("typeSystemVersion");
	            }
	
	            cadenaConexion= "user id = " + id +"; password = " + password + "; server = " + server + "; database = " +
	                database + "; connection timeout = " + connectionTimeOut;// + "; integrated Security = " + integratedSecurity + 
//	                "; multipleActiveResultSets = " + multipleActiveResultSets + "; packet Size = " + packetSize + "; persist Security Info = " + 
//	                persistSecurityInfo + "; pooling = " + pooling + " ;type System Version = " + typeSystemVersion;
        	}
        	catch (Exception ex)
        	{
        		MessageBox.Show(ex.ToString(),"?");
        	}

            try
            {
                connectionManager = new ConnectionManager();
                String connStr = cadenaConexion;
                connectionManager.connection = new SqlConnection(connStr);
                connectionManager.connection.Open();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
            }
        }
				
	}
}
